#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    tcpServer = new QTcpServer(this);
    this->setWindowTitle("服务端");
    //新的连接
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(mNewConnetion()));
     ui->pushButton_2->setEnabled(false);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::mNewConnetion()
{
    //与客户端连接
    QTcpSocket *tmpTcpSocket = tcpServer->nextPendingConnection();//等待连接的方法

    //客户端的IP地址
    QString ipaddr = tmpTcpSocket->peerAddress().toString();
    quint16 port = tmpTcpSocket->peerPort();


    //打印客户端连接的ip消息
    ui->textBrowser->append("服务端：客户端的IP地址："+ipaddr);
    //打印客户端连接的端口信息
    ui->textBrowser->append("服务端：客户端的端口："+ QString::number(port));

    connect(tmpTcpSocket,SIGNAL(readyRead()),this,SLOT(receiveMessages()));
    connect(tmpTcpSocket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),
            this,SLOT(mStateChanged(QAbstractSocket::SocketState)));
}

void Widget::receiveMessages()
{
    QTcpSocket *tmpTcpSocket = (QTcpSocket *)sender();
    //打印接收的消息
    ui->textBrowser->append("客户端："+tmpTcpSocket->readAll());
}

void Widget::mStateChanged(QAbstractSocket::SocketState state)
{
    QTcpSocket *tmpTcpSocket = (QTcpSocket *)sender();
    switch(state){
        //已经断开
    case QAbstractSocket::UnconnectedState:
        ui->textBrowser->append("服务端：客户端断开连接");
       // delete tmpTcpSocket;//销毁对象   错了就注释这里
        tmpTcpSocket->deleteLater();//延时一段就删除
        break;
        //已经连接
    case QAbstractSocket::ConnectedState:
        ui->textBrowser->append("服务端：客户端已连接");
        break;

    default:
        break;
    }

}

void Widget::on_pushButton_3_clicked()
{
    QList <QTcpSocket *> socketList =  tcpServer->findChildren <QTcpSocket *>();
    qDebug() << "tcpSocket 数量" << socketList.count()<<endl;

    if(socketList.count()==0){

        ui->textBrowser->append("当前没有客户端连接，请先与客户端连接");
        return  ;
    }
    foreach (QTcpSocket *tmpTcpSocket, socketList) {
        //服务端向每个客户端发送消息
        tmpTcpSocket->write(ui->lineEdit->text().toUtf8());
    }
     ui->textBrowser->append("服务端："+ui->lineEdit->text());
}


void Widget::on_pushButton_clicked()
{
    //绑定IP地址和端口
    tcpServer->listen(QHostAddress("172.16.200.3"),9999);

      ui->textBrowser->append("服务端：监听的ip地址和端口：");
}


void Widget::on_pushButton_2_clicked()
{
    // 停止监听
    tcpServer->close();


}
